/** 
 * @description 工具集
 * @author c-jw
 * @time 2020年3月31日 15:05:49
 */

/**
 * @desc 随机名称
 */
export const makeRandomName = function (name = '') {
  const add0 = (m) => {
    return m < 10 ? '0' + m : m;
  }
  const timestamp = () => {
    var time = new Date();
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    var mills = time.getMilliseconds();

    return `${y}${add0(m)}${add0(d)}${add0(h)}${add0(mm)}${add0(s)}${mills}`;
  }
  return `${name}_${Math.round(Math.random() * 1000)}_${timestamp()}`;
};
/**
 * @desc 日期格式化
 */
export const dateFormat = function (time, fmt) {
  var date = time ? new Date(time) : new Date();
  fmt = fmt || "yyyy-MM-dd hh:mm:ss"
  var o = {
    "M+": date.getMonth() + 1, //月份   
    "d+": date.getDate(), //日   
    "h+": date.getHours(), //小时   
    "m+": date.getMinutes(), //分   
    "s+": date.getSeconds(), //秒   
    "q+": Math.floor((date.getMonth() + 3) / 3), //季度   
    "S": date.getMilliseconds() //毫秒   
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};
/**
 * @desc 阻止事件冒泡
 */
export const preventEvent = function (ev) {
  ev.preventDefault();
  ev.stopPropagation();
};
/**
 * @desc 个性日志
 */
export const log = function (o) {
  console.log("%c %s %O", "color:red", "[连接数据库失败]", o);
}